﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models;
using DAL.User;
using DAL;

namespace BLL.User
{
    public class UserService : BaseServicePro<Models.User>,IUserService
    {
        UserDao userDao = new UserDao();

        private string createNickName(Models.User user)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
            long timeStamp = (long)(DateTime.Now - startTime).TotalSeconds; // 相差秒数
            return "昵称_" + user.UserID + timeStamp;
        }

        /// <summary>
        /// 创建用户
        /// </summary>
        /// <param name="user">用户实体类</param>
        /// <param name="userInfo">用户信息实体类</param>
        /// <param name="resumeInfo">用户简历信息实体类</param>
        /// <returns>true创建成功，反之创建失败</returns>
        public bool createUser(Models.User user, UserInfo userInfo, ResumeInfo resumeInfo)
        {
            userDao.saveUser(user);
            userInfo.UserID = user.UserID;

            userInfo.NickName = createNickName(user);

            resumeInfo.OwnerID = user.UserID;

            userDao.saveUserInfo(userInfo);
            userDao.saveResumeInfo(resumeInfo);

            if (!(user.UserID == null && userInfo.UserID == null && resumeInfo.OwnerID == null))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 通过用户名，判断用户名是否存在
        /// </summary>
        /// <param name="username">用户名</param>
        /// <returns>如果存在返回用户实体类，null则不存在</returns>
        public Models.User ExistUserByUserName(string username)
        {
            return userDao.QueryUserByUserName(username);
        }

        /// <summary>
        /// 通过用户名和密码，判断用户是否存在
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="pwd">密码</param>
        /// <returns>用户实体类</returns>
        public Models.User ExistUserByUserNameAndPwd(string username, string pwd)
        {
            return userDao.QueryUserByUserNameAndPwd(username, pwd);
        }

        /// <summary>
        /// 获取用户简历信息
        /// </summary>
        /// <param name="user">用户实体类</param>
        /// <returns>用户简历信息实体类</returns>
        public ResumeInfo GetResumeInfo(Models.User user)
        {
            return userDao.QueryResumeInfo(user);
        }

        /// <summary>
        /// 获取用户资料信息
        /// </summary>
        /// <param name="user">用户实体类</param>
        /// <returns>用户资料信息</returns>
        public UserInfo GetUserInfo(Models.User user)
        {
            return userDao.QueryUserInfo(user);
        }

        /// <summary>
        /// 更新用户简历信息到数据库
        /// </summary>
        /// <param name="resumeInfo">用户简历信息实体类</param>
        /// <returns>
        /// true创建成功，反之创建失败
        /// </returns>
        public bool updateResumeInfo(ResumeInfo resumeInfo)
        {
            return userDao.updateResumeInfo(resumeInfo);
        }

        /// <summary>
        /// 更新用户到数据库
        /// </summary>
        /// <param name="user">用户实体类</param>
        /// <returns>true更新成功，反之更新失败</returns>
        public bool updateUser(Models.User user)
        {
            return userDao.updateUser(user);
        }

        /// <summary>
        /// 更新用户信息到数据库
        /// </summary>
        /// <param name="userInfo">用户信息实体类</param>
        /// <returns>true更新更改，反之更新失败</returns>
        public bool updateUserInfo(UserInfo userInfo)
        {
            userInfo.UpdatedAt = DateTime.Now;
            return userDao.updateUserInfo(userInfo);
        }

        public List<Models.User> GetList(ref int totalCount, int pageIndex = 1, int pageSize = 12, string orderBy = "CreatedAt Desc")
        {
            List<Models.User> list = userDao.GetList(ref totalCount, pageIndex, pageSize, orderBy);

            return list;
        }

        public List<Models.User> GetList(ref int totalCount, string[] tags, int pageIndex = 1, int pageSize = 12, string orderBy = "CreatedAt Desc")
        {
            List<Models.User> list = userDao.GetList(ref totalCount, tags, pageIndex, pageSize, orderBy);

            return list;
        }

        protected override BaseDaoPro<Models.User> GetDao()
        {
            return new UserDao();
        }
    }
}
